package com.joint.web.action.mp;

import com.fz.us.base.bean.BaseEnum;
import com.fz.us.base.bean.Pager;
import com.google.common.collect.Maps;
import com.joint.base.service.UsersService;
import com.joint.base.util.StringUtils;
import com.joint.core.entity.Client;
import com.joint.core.service.ClientService;
import com.joint.web.action.BaseController;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dqf on 2015/3/9.
 */
@Controller
@RequestMapping(value = "/mp")
public class AjaxClientController extends BaseController{
    @Resource
    private ClientService clientService;
    @Resource
    UsersService usersService;

    private List<Client> clientList;
    private Client client;
    private String searchString;


    @RequestMapping(value = "/ajax-client/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> list(int page,String pageKeyword){
        List<JSONObject> dataRows = new ArrayList<JSONObject>();
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String, Object> map = Maps.newHashMap();
        Map<String,Object> rmap;
        map.put("state", new BaseEnum.StateEnum[]{BaseEnum.StateEnum.Enable});
        Pager pager = new Pager();
        pager.setPageSize(10);
        pager.setPageNumber(page);
        pager.setOrderBy("createDate");
        pager.setOrderType(BaseEnum.OrderType.desc);
        pager.setPageNumber(page);
        if(StringUtils.isNotEmpty(pageKeyword)){
            map.put("name", pageKeyword);
            pager.setKeyword(pageKeyword);
        }
        pager = clientService.findByPagerAndCompany(pager,null,usersService.getCompanyByUser(),map);
        clientList = (List<Client>) pager.getList();
        for(Client client:clientList){
            rmap=Maps.newHashMap();
            rmap.put("id",client.getId());
            rmap.put("name",client.getName());
            rmap.put("url",getPath()+"mp/ajax-client/read/"+client.getId());
            rmap.put("subName",client.getSubName());
            rmap.put("type",client.getType().getName());
            dataRows.add(JSONObject.fromObject(rmap));
        }
        data.put("pageNum", pager.getPageNumber());//页数
        data.put("pageSize", dataRows.size());//记录数
        data.put("pageCount", pager.getPageCount());//总页数
        data.put("totalCount", pager.getTotalCount());//总记录数
        data.put("pageKeyword", pager.getKeyword()==null?"": pager.getKeyword());//查询
        data.put("dataRows", dataRows);//数据
        return ajaxHtmlAppResult(1, "", JSONObject.fromObject(data));
    }

    @RequestMapping(value = "/ajax-client/read/{keyId}", method = RequestMethod.GET)
    public String read(Model model,@PathVariable("keyId") String keyId){
        if(StringUtils.isEmpty(keyId)) return "/mp/index";
        client = clientService.get(keyId);
        model.addAttribute("client",client);
        return "/mp/ajax-client_read";
    }

    public List<Client> getClientList() {
        return clientList;
    }

    public void setClientList(List<Client> clientList) {
        this.clientList = clientList;
    }

    public Client getClient() {
        return client;
    }

    public void setClient(Client client) {
        this.client = client;
    }

    public String getSearchString() {
        return searchString;
    }

    public void setSearchString(String searchString) {
        this.searchString = searchString;
    }
}
